package com.foreverwin.jzymes.production.remedy.service.impl;

import com.foreverwin.jzymes.common.domain.AgvReqLog;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.cnc.service.ICncSfcPassAdviceService;
import com.foreverwin.jzymes.production.remedy.service.IRemedyAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class RemedyAdviceServiceImpl implements IRemedyAdviceService {

    @Autowired
    ICncSfcPassAdviceService cncSfcPassAdviceService;

    @Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    public void save( String site, String sourceTranship, String sourceItemType, String targetTranship, String targetItemType, Long trayQty ){

        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( sourceTranship, I18nUtil.getI18nText("production.remedy.sourceTranshipNotBlank") );
        Assert.hasText( sourceItemType, I18nUtil.getI18nText("production.remedy.sourceItemTypeNotBlank") );
        Assert.hasText( targetTranship, I18nUtil.getI18nText("production.remedy.targetTranshipNotBlank") );
        Assert.hasText( targetItemType, I18nUtil.getI18nText("production.remedy.targetItemTypeNotBlank") );
        Assert.isTrue( trayQty > 0, I18nUtil.getI18nText( "production.remedy.trayQtyNotValid", new Object[]{ trayQty } ) );

        /**源接驳站数据处理*/
        AgvReqLog agvReqLog = new AgvReqLog();
        agvReqLog.setSite( site );
        agvReqLog.setTranship( sourceTranship );
        agvReqLog.setTrayQty( trayQty );
        agvReqLog.setItemType( sourceItemType );
        agvReqLog.setSendTime( new Date() );
        cncSfcPassAdviceService.sfcPassByTrayQty( agvReqLog );

        /**目标接驳站数据处理*/
        agvReqLog = new AgvReqLog();
        agvReqLog.setSite( site );
        agvReqLog.setTranship( targetTranship );
        agvReqLog.setTrayQty( trayQty );
        agvReqLog.setItemType( targetItemType );
        agvReqLog.setSendTime( new Date() );
        cncSfcPassAdviceService.sfcPassByTrayQty( agvReqLog );
    }

}
